package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MessageSearchActivity;
import flc.ast.adapter.DiscoverAdapter;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.TopicAdapter;
import flc.ast.databinding.FragmentMessageBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MessageFragment extends BaseNoModelFragment<FragmentMessageBinding> {

    private TopicAdapter topicAdapter;
    private DiscoverAdapter discoverAdapter;
    private HotAdapter hotAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llSearch.setOnClickListener(this);
        mDataBinding.tvChange.setOnClickListener(this);

        mDataBinding.rvTopicList.setLayoutManager(new GridLayoutManager(mContext, 2));
        topicAdapter = new TopicAdapter();
        mDataBinding.rvTopicList.setAdapter(topicAdapter);
        topicAdapter.setOnItemClickListener(this);

        mDataBinding.rvDiscoverList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        discoverAdapter = new DiscoverAdapter();
        mDataBinding.rvDiscoverList.setAdapter(discoverAdapter);
        discoverAdapter.setOnItemClickListener(this);

        mDataBinding.rvHotList.setLayoutManager(new LinearLayoutManager(mContext));
        hotAdapter = new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);

        mDataBinding.refreshHotItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshHotItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshHotItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getHotData();
                mDataBinding.refreshHotItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getHotData();
                mDataBinding.refreshHotItem.finishRefresh(refreshTime);
            }
        });

    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSearch://搜索
                startActivity(MessageSearchActivity.class);
                break;
            case R.id.tvChange://换一换
                getTopicData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getTopicData();
        getDiscoverData();
        getHotData();
    }

    private void getTopicData() {
        StkResApi.getTagResourceList(this, AppConfig.TOPIC_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        topicAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getDiscoverData() {
        StkResApi.getTagResourceList(this, AppConfig.DISCOVER_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        discoverAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getHotData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            hotAdapter.setList(result);
                        } else {
                            hotAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

}